<?php
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
			//get one birth based on id
			get_birth();
			break;
		case 2:
			update_birth();
			break;	
		case 3:
			add_birth();
			break;	
		
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	
	function get_birth(){
	include_once("birth.php");
		
		$id=$id=get_datan("id");
		$v=new birth();
		$v->get_birth($id);
		$row=$v->fetch();
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","birth info not found");
			echo "}";
			return;
		}
		
		echo "{";
			echo jsonn("result",1) .",";
			echo '"birth":{';
			echo jsonn("id",$id).",";
			//name
				echo jsons("name", $row["name"]). ",";
			//name
				echo jsons("gender", $row["gender"]). ",";
			//schedule
				echo jsons("date_of_birth", $row["date_of_birth"]). ",";
			//url
				echo jsons("place_of_birth", $row["place_of_birth"]);

			echo "}";
		echo "}";
		return;
	}
	
	function add_birth(){
		include_once("birth.php");
		//$id=get_datan("id");
		$name=get_data('bn');
		$gender=get_data('bg');
		$place_of_birth=get_data('bp');
		$date_of_birth=get_data('bd');

		$b=new birth();
		if(!$b->add_birth($name,$gender,$place_of_birth,$date_of_birth)){
			echo'{"result":0,"message":"id not found"}';		
		}
		else{
			echo'{"result":1,"message":"new birth recorded"}';
			}		
	}
	
	
	function update_birth(){
		include_once("birth.php");
		$id=get_datan("id");
		$name=get_data('bn');
		$gender=get_data('bg');
		$date_of_birth=get_data('bd');
		$place_of_birth=get_data('bp');
		
		if(!$id){
			//return error message
			echo'{"result":0,"message":"id not found"}';
			}
		
		$b=new birth();
		if(!$b->update_birth($id,$name,$gender,$date_of_birth,$place_of_birth)){
			echo'{"result":0,"message":"id not found"}';		
		}
		else{
			echo'{"result":1,"message":"update successful"}';
			}		
	}
	
	
	